package com.iot.inter.interfaceprocessing.newinterface;

import com.iot.inter.entity.common.DeviceFlowUsage;
import com.iot.inter.entity.common.OrderPaymentManager;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newpojo.TurntoInterfaceUtils;
import com.iot.inter.entity.newpojo.request.RequestBody.*;
import com.iot.inter.entity.newpojo.request.RequestHeader;
import com.iot.inter.entity.newpojo.request.subRequest.*;
import com.iot.inter.entity.newpojo.response.subResponse.*;
import com.iot.inter.utils.HttpClientTool;
import com.iot.inter.utils.JaxbUtil;
import org.apache.log4j.Logger;

/**
 * 去北京做：查询流量使用情况:2.0接口
 *
 * @author lm
 * @date 2019/8/5
 */
public class TurnTrafficUsageService {
    private static Logger logger = Logger.getLogger(TurnTrafficUsageService.class);

    /**
     * 去北京做：3.18查询单一设备流量使用情况
     *
     * @author lm
     * @date 2019/8/5
     */
    public OrderPaymentManager singleDevice(String deviceId, String secret, String partnerCode) {

        try {
            //请求body参数处理
            SingleDeviceTrafficUsageRequestBody body = new SingleDeviceTrafficUsageRequestBody();
            body.setDeviceId(deviceId);

            //参数拼接
            SingleDeviceTrafficUsageRequest request = new SingleDeviceTrafficUsageRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            RequestHeader header = utils.getRequestHeader(Constant.cmd_SingleDeviceTrafficUsage, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(SingleDeviceTrafficUsageRequest.class).toXml(request, "UTF-8");
            logger.info("【查询单一设备流量使用情况】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_SingleDeviceTrafficUsage);

            logger.info("【查询单一设备流量使用情况】xml响应：" + result);
            if (null != result && !result.equals("-1")) {
                SingleDeviceTrafficUsageResponse responseObj = new JaxbUtil(SingleDeviceTrafficUsageResponse.class).fromXml(result, "utf-8");
                if (null != responseObj) {

                    // 返回错误结果的处理
                    OrderPaymentManager re = new OrderPaymentManager();
                    re.setRetCode(responseObj.getHeader().getResultCode());
                    re.setResultInfo(responseObj.getHeader().getResultInfo());
                    re.setDeviceId(responseObj.getBody().getDeviceId());
                    re.setOrderId(responseObj.getBody().getOrderCode());
                    re.setOrder_used_flow(responseObj.getBody().getOrderUsedFlow());
                    re.setHigh_flow_balance(responseObj.getBody().getHighFlowBalance());
                    return re;

                } else {
                    // 返回错误结果的处理
                    OrderPaymentManager re = new OrderPaymentManager();
                    re.setRetCode("9999");
                    re.setResultInfo("返回异常");
                    return re;
                }


            } else {
                // 返回错误结果的处理
                OrderPaymentManager re = new OrderPaymentManager();
                re.setRetCode("9999");
                re.setResultInfo("返回异常");
                return re;
            }

        } catch (Exception e) {
            OrderPaymentManager re = new OrderPaymentManager();
            re.setRetCode("9999");
            re.setResultInfo("捕获到处理过程中的异常");
            return re;
        }
    }

    /**
     * 去北京做：3.7	查询单日流量使用情况 ：接口暂时不能使用
     *
     * @author lm
     * @date 2019/8/5
     */
    public SingleDayTrafficUsageResponse singleDay(Integer pageNo, Integer pageSize, String date, String secret, String partnerCode) {

        try {
            //请求body参数处理
            SingleDayTrafficUsageRequestBody body = new SingleDayTrafficUsageRequestBody();
            body.setPageNo(pageNo);
            body.setPageSize(pageSize);
            body.setDate(date);
            //参数拼接
            SingleDayTrafficUsageRequest request = new SingleDayTrafficUsageRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            RequestHeader header = utils.getRequestHeader(Constant.cmd_SingleDayTrafficUsage, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(SingleDayTrafficUsageRequest.class).toXml(request, "UTF-8");
            logger.info("【查询单日流量使用情况】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_SingleDayTrafficUsage);

            logger.info("【查询单日流量使用情况】xml响应：" + result);

            if (result != null && !result.equals("-1")) {

                SingleDayTrafficUsageResponse responseObj = new JaxbUtil(SingleDayTrafficUsageResponse.class).fromXml(result, "utf-8");

                if (responseObj != null) {
                    // 返回错误结果的处理
                    SingleDayTrafficUsageResponse response = new SingleDayTrafficUsageResponse();
                    response.setBody(responseObj.getBody());
                    response.setRetCode(responseObj.getHeader().getResultCode());
                    response.setResultInfo(responseObj.getHeader().getResultInfo());
                    return response;

                } else {
                    // 返回错误结果的处理
                    SingleDayTrafficUsageResponse response = new SingleDayTrafficUsageResponse();
                    response.setRetCode("9998");
                    response.setResultInfo("响应为空！");
                    return response;
                }


            } else {
                // 返回错误结果的处理
                SingleDayTrafficUsageResponse response = new SingleDayTrafficUsageResponse();
                response.setRetCode("9998");
                response.setResultInfo("响应为-1！");
                return response;
            }

        } catch (Exception e) {

            SingleDayTrafficUsageResponse response = new SingleDayTrafficUsageResponse();
            response.setRetCode("9999");
            response.setResultInfo("响应异常！");
            return response;
        }

    }

    /**
     * 去北京做：3.6	查询指定设备一定时间范围内的流量使用情况
     *
     * @author lm
     * @date 2019/8/27
     */
    public QuerDeviceDayFlowResponse appointDeviceDayTrafficUsage(String deviceId, String startDay, String endDay, String partnerCode, String secret) {

        try {
            QuerDeviceDayFlowRequestBody body = new QuerDeviceDayFlowRequestBody();
            body.setDeviceId(deviceId);
            body.setStartDay(startDay);
            body.setEndDay(endDay);

            // 请求参数拼接
            QuerDeviceDayFlowRequest request = new QuerDeviceDayFlowRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_singleDeviceDayFlow, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(QuerDeviceDayFlowRequest.class).toXml(request, "utf-8");
            logger.info("【查询指定设备一定时间范围内的流量使用情况】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_singleDeviceDayFlow);

            logger.info("【查询指定设备一定时间范围内的流量使用情况】xml响应：" + result);

            QuerDeviceDayFlowResponse responseObj = new JaxbUtil(QuerDeviceDayFlowResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
                QuerDeviceDayFlowResponse response = new QuerDeviceDayFlowResponse();
                response.setRetCode(retCode);
                response.setResultInfo(resultInfo);
                return response;

            } else {

                QuerDeviceDayFlowResponse response = new QuerDeviceDayFlowResponse();
                response.setRetCode(retCode);
                response.setResultInfo(resultInfo);
                return response;
            }


        } catch (Exception e) {

            QuerDeviceDayFlowResponse response = new QuerDeviceDayFlowResponse();
            response.setRetCode("9999");
            response.setResultInfo("捕获到处理过程中的异常");
            return response;

        }

    }

    /**
     * 去北京做：3.5	查询指定设备指定月份的流量使用情况
     * <p>
     * 1，指定了设备，指定了月份，查询流量的适用情况
     *
     * @author lm
     * @date 2019/8/27
     */
    public DeviceFlowUsage appointDeviceMonthTrafficUsage(String deviceId, String month, String partnerCode, String secret) {
        try {
            QuerySpecifiedDeviceAndMonthRequestBody body = new QuerySpecifiedDeviceAndMonthRequestBody();
            body.setDeviceId(deviceId);
            body.setMonth(month);

            // 请求参数拼接
            QuerySpecifiedDeviceAndMonthRequest request = new QuerySpecifiedDeviceAndMonthRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_singleDeviceMonthFlow, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(QuerySpecifiedDeviceAndMonthRequest.class).toXml(request, "utf-8");
            logger.info("【查询指定设备指定月份的流量使用情况】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_singleDeviceMonthFlow);

            logger.info("【查询指定设备指定月份的流量使用情况】xml响应：" + result);

            QuerySpecifiedDeviceAndMonthResponse responseObj = new JaxbUtil(QuerySpecifiedDeviceAndMonthResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode) && responseObj.getBody().getFlow() != null) {
                //成功，拿到回传的结果回去处理
                DeviceFlowUsage flowUsage = new DeviceFlowUsage();
                flowUsage.setFlow(responseObj.getBody().getFlow());
                flowUsage.setRetCode(retCode);
                flowUsage.setResultInfo(resultInfo);
                return flowUsage;
            } else {
                DeviceFlowUsage flowUsage = new DeviceFlowUsage();
                flowUsage.setRetCode(retCode);
                flowUsage.setResultInfo(resultInfo);
                return flowUsage;
            }

        } catch (Exception e) {

            DeviceFlowUsage flowUsage = new DeviceFlowUsage();
            flowUsage.setRetCode("9999");
            flowUsage.setResultInfo("响应异常！");
            return flowUsage;
        }
    }

    /**
     * 去北京做：3.4	批量查询指定月份的流量使用情况
     * <p>
     * 接入方可以通过调用该接口，批量查询其拥有的所有设备信息在指定月份流量使用情况。
     * 考虑到拥有的设备数量会很多，所以本接口按照分页形式，每次查询仅获得一页设备流量使用信息，其中每页包含查询结果数量，由接入方自行定义。
     * 若要获取所有设备的流量使用情况，则需要多次调用该接口。
     *
     * @author lm
     * @date 2019/8/27
     */
    public BatchAppointMonthResponse batchAppointMonthTrafficUsage(Integer pageNo, Integer pageSize, String month, String partnerCode, String secret) {

        try {
            BatchAppointMonthRequestBody body = new BatchAppointMonthRequestBody();

            if (pageNo != null) {
                body.setPageNo(pageNo.toString());
            } else {
                body.setPageNo("1");
            }
            if (pageNo != null) {
                body.setPageSize(pageSize.toString());
            } else {
                body.setPageSize("20");
            }
            body.setMonth(month);

            // 请求参数拼接
            BatchAppointMonthRequest request = new BatchAppointMonthRequest();

            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_totalMonthFlowList, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(BatchAppointMonthRequest.class).toXml(request, "utf-8");
            logger.info("【批量查询指定月份的流量使用情况】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_totalMonthFlowList);

            logger.info("【批量查询指定月份的流量使用情况】xml响应：" + result);

            BatchAppointMonthResponse responseObj = new JaxbUtil(BatchAppointMonthResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (responseObj != null) {

                BatchAppointMonthResponse res = new BatchAppointMonthResponse();
                res.setBody(responseObj.getBody());
                res.setRetCode(retCode);
                res.setResultInfo(resultInfo);
                return res;

            } else {

                BatchAppointMonthResponse res = new BatchAppointMonthResponse();
                res.setRetCode("9998");
                res.setResultInfo("响应为空");
                return res;

            }

        } catch (Exception e) {
            BatchAppointMonthResponse res = new BatchAppointMonthResponse();
            res.setRetCode("9999");
            res.setResultInfo("响应异常");
            return res;
        }
    }
}